package com.ssl.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author shi_senlin
 * @create 2021/04/24 18:44
 */
public class MybatisPlusGenerator {
	private static String projectName = "/spring-security";
	private static String projectBaseDir = System.getProperty("user.dir").concat(projectName);
	/**
	 * 自动生成代码输出目录
	 */
	private static String javaOutputDir = projectBaseDir.concat("/src/main/java");

	private static String parent = "com.ssl.spring";
	private static String parent_ = "com/ssl/spring/";
	private static String moudle = "security";
	private static String tablePrefix = "t_";
	/**
	 * 作者
	 */
	private static String author = "ssl";

	private static String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/javabasics-security?useUnicode=true&useSSL=false" +
			"&characterEncoding=utf8";
	private static String driver = "com.mysql.cj.jdbc.Driver";
	private static String username = "root";
	private static String password = "123456";

	static AutoGenerator mpg = new AutoGenerator();

	public static void main(String[] args) {
		generator();
		System.out.println("success");

	}

	private static void generator() {
		// 全局配置
		GlobalConfig globalConfig = new GlobalConfig();
		System.out.println(javaOutputDir);
		globalConfig.setOutputDir(javaOutputDir);
		globalConfig.setAuthor(author);
		globalConfig.setOpen(false);
		mpg.setGlobalConfig(globalConfig);
		// 数据源配置
		DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setUrl(jdbcUrl);
		dataSourceConfig.setDriverName(driver);
		dataSourceConfig.setUsername(username);
		dataSourceConfig.setPassword(password);
		mpg.setDataSource(dataSourceConfig);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setModuleName(moudle);
		pc.setParent(parent);
		mpg.setPackageInfo(pc);

		// 自定义输出配置(配置输出的xml)
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
			}
		};
		String templatePath = "/templates/mapper.xml.vm";
		List<FileOutConfig> focList = new ArrayList<>();
		// 自定义配置会被优先输出
		focList.add(new FileOutConfig(templatePath) {
			@Override
			public String outputFile(TableInfo tableInfo) {
				return projectBaseDir + "/src/main/resources/" + parent_ + moudle + "/mapping/"
						+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);

		// 配置模板
		TemplateConfig templateConfig = new TemplateConfig();
		templateConfig.setXml(null);
		mpg.setTemplate(templateConfig);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		strategy.setControllerMappingHyphenStyle(true);
		// strategy.setInclude(""); // 和 setTablePrefix 二选一
		strategy.setTablePrefix(tablePrefix);
		mpg.setStrategy(strategy);
		mpg.execute();
	}
}
